﻿#region Apache License
/* 
   Copyright 2011 Jorge Teixeira

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace AdHoc.Wavesque.Test
{
    [TestFixture(Description = "Unit tests for class Trianglesque.")]
    public class TrianglesqueTest
    {
        [Test(Description = "Create an object representation of a triangle wave.")]
        public void Create()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Step, TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);
            Assert.IsInstanceOf(typeof(Trianglesque), f);
        }

        [Test(Description = "Create an object representation of a triangle wave.")]
        public void Create2()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);
            Assert.IsInstanceOf(typeof(Trianglesque), f);
        }

        [Test(Description = "Creates a synchronized object representation of a triangle wave.")]
        public void Create3()
        {
            var f = Trianglesque.Synchronized(new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase));
            Assert.IsInstanceOf(typeof(Trianglesque), f);
            Assert.IsTrue(f.IsSynchronized);
        }

        [Test(Description = "Generate a default number of points of a triangle wave.")]
        public void GetValuesTest()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a triangle wave.")]
        public void GetValuesTest2()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(out x, out y, TrianglesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a given number of points of a triangle wave.")]
        public void GetValuesTest3()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(TrianglesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(TrianglesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a triangle wave.")]
        public void GetValuesTest4()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(TrianglesqueTestCaseData.NumberOfPoints, out x, out y, TrianglesqueTestCaseData.X0);

            Assert.AreEqual(TrianglesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a triangle wave.")]
        public void GetValuesTest5()
        {
            var f = Trianglesque.Synchronized(new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase));

            Assert.IsTrue(f.IsSynchronized);

            double[] x;
            double[] y;
            f.GetValues(TrianglesqueTestCaseData.NumberOfPoints, out x, out y, TrianglesqueTestCaseData.X0);

            Assert.AreEqual(TrianglesqueTestCaseData.NumberOfPoints, y.Length);
        }
       
        [Test(Description = "Generate a given number of points of a triangle wave."), TestCaseSource(typeof(TrianglesqueTestCaseData), "Data")]
        public void GetValuesTest(double step, double amplitude, double frequency, double phase, double x0, uint numberOfPoints)
        {
            double[] aX;
            double[] aY;

            var f = new Trianglesque(step, amplitude, frequency, phase);
            f.GetValues(numberOfPoints, out aX, out aY, x0);
            
            Assert.AreEqual(numberOfPoints, aY.Length);
            
            List<double> bX = new List<double>();
            List<double> bY = new List<double>();
                        
            foreach(double pos in aX)
            {
            	double[] x;
            	double[] y;
            	var g = new Trianglesque(step, amplitude, frequency, phase);
            	g.GetValues(1, out x, out y, pos);
            	
            	Assert.AreEqual(1, x.Length);
            	Assert.AreEqual(1, y.Length);
            	
            	bX.AddRange(x);
            	bY.AddRange(y);
            }
            
            Assert.AreEqual(aX, bX);
            Assert.AreEqual(aY, bY);
        }

        [Test(Description = "Generate a given number of points of a triangle wave, half wave rectification.")]
        public void ToHalfWaveRectification()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(TrianglesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(TrianglesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a triangle wave, half wave rectification.")]
        public void ToHalfWaveRectification2()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(TrianglesqueTestCaseData.NumberOfPoints, out x, out y, TrianglesqueTestCaseData.X0);

            Assert.AreEqual(TrianglesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a default number of points of a triangle wave, half wave rectification.")]
        public void ToHalfWaveRectification3()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a triangle wave, half wave rectification.")]
        public void ToHalfWaveRectification4()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(out x, out y, TrianglesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a given number of points of a triangle wave, full wave rectification.")]
        public void ToFullWaveRectification()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(TrianglesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(TrianglesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a triangle wave, full wave rectification.")]
        public void ToFullWaveRectification2()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(TrianglesqueTestCaseData.NumberOfPoints, out x, out y, TrianglesqueTestCaseData.X0);

            Assert.AreEqual(TrianglesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a triangle wave, full wave rectification.")]
        public void ToFullWaveRectification3()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a triangle wave, full wave rectification.")]
        public void ToFullWaveRectification4()
        {
            var f = new Trianglesque(TrianglesqueTestCaseData.Amplitude, TrianglesqueTestCaseData.Frequency, TrianglesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(out x, out y, TrianglesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }
    }
}
